package com.lilosoft.business.precity.service;

import com.lilosoft.business.cga.models.CgaCaseRelatedPerson;
import com.lilosoft.business.precity.dao.UpProAcceptIndividualMapper;
import com.lilosoft.business.precity.models.UpProAccept;
import com.lilosoft.business.precity.models.UpProAcceptIndividual;
import com.lilosoft.business.precity.models.UpProAcceptIndividualExample;
import com.lilosoft.core.utils.commons.UUIDUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:  自然人信息
 * User: 小峰
 * Date: 2019-08-28
 * Time: 9:45
 */
@Service
public class UpProAcceptIndividualService {
    @Autowired
    private UpProAcceptIndividualMapper upProAcceptIndividualMapper;

    /**
     * 同步申请自然人信息
     * @param upProAccept
     * @param cgaPerson
     * @return
     */
    public UpProAcceptIndividual UpProAcceptIndividualSynchronous(UpProAccept upProAccept, CgaCaseRelatedPerson cgaPerson){
        UpProAcceptIndividual model = new UpProAcceptIndividual();
        BeanUtils.copyProperties(upProAccept,model);
        model.setRowguid(UUIDUtils.createUUId());
        model.setProjectRowguid(upProAccept.getRowguid());
        model.setProjectno(upProAccept.getProjectno());
        model.setApplyertype("1");
        model.setApplyername(cgaPerson.getRelatedPersonName());
        model.setApplyerpagetype(upProAccept.getApplyerpagetype());
        model.setApplyerpagecode(upProAccept.getApplyerpagecode());
        model.setApplyerpagePhone(cgaPerson.getMobile());
        model.setContactmobile(cgaPerson.getMobile());

        List<UpProAcceptIndividual> list = selectByProjectNo(model.getProjectno());
        if(null== list || list.size()<=0){
            upProAcceptIndividualMapper.insertSelective(model);
        }
        return model;
    }

    public  List<UpProAcceptIndividual> selectByProjectNo(String projectNo){
        UpProAcceptIndividualExample example= new UpProAcceptIndividualExample();
        UpProAcceptIndividualExample.Criteria criteria = example.createCriteria();
        criteria.andProjectnoEqualTo(projectNo);
        criteria.andDelStateEqualTo(0);
        return upProAcceptIndividualMapper.selectByExample(example);
    }

    /**
     * 根据cdBatch 统计数据个数（自然人信息）
     * @param cdBatch
     * @return
     */
    public Long countByCDbatch(String cdBatch){
        UpProAcceptIndividualExample example= new UpProAcceptIndividualExample();
        UpProAcceptIndividualExample.Criteria criteria = example.createCriteria();
        criteria.andCdBatchEqualTo(cdBatch);
        return upProAcceptIndividualMapper.countByExample(example);
    }
}
